import pymysql,datetime
from pymysql.cursors import DictCursor
from pexpect import pxssh
import time
con=pymysql.connect(host='rm-j6c7o95747bp853w0so.mysql.rds.aliyuncs.com',user='qiqi',password='Qiqi2018_',database='qhjy',charset="utf8",cursorclass=DictCursor)
def aaa():
    with con as cur:
        sql = "select qh_result.exchange_name,qh_nper.id  from qh_result INNER  join qh_nper on qh_result.nper_id=qh_nper.id where qh_nper.coin_id=24 and qh_nper.is_current = 1 and qh_result.exchange_name in (1,2,3,4,7) GROUP by qh_result.exchange_name ORDER by qh_result.exchange_name"
        cur.execute(sql)
        res=cur.fetchall()
        print(res)
        if len(res) >= 5:
            pass
        else:
            print("ETH交割失败")
            mlst=[1, 2, 3, 4, 7]
            for r in res:
                if int(r["exchange_name"]) in mlst:
                    mlst.remove(int(r["exchange_name"]))
            for l in mlst:
                sql = "insert into qh_result(nper_id, exchange_name, coin_id,open_time_price,end_time_price) VALUES({},{},{},{},{})".format(int(r["id"]), l, 24,0,0)
                cur.execute(sql)
def bbb():
    severip="116.206.176.93"
    user="root"
    password="123456."
    sever=pxssh.pxssh()#创建一个远程登陆对象
    sever.login(severip,user,password)#登陆
    sever.sendline("ps -ef|grep huobi_eth|awk '{print $2}'|xargs kill -9")#执行命令
    sever.prompt()#等待命令执行完成
    sever.sendline("ps -ef|grep binance_eth|awk '{print $2}'|xargs kill -9")#执行命令
    sever.prompt()#等待命令执行完成
    sever.sendline("ps -ef|grep bithumb_eth|awk '{print $2}'|xargs kill -9")#执行命令
    sever.prompt()#等待命令执行完成
    sever.sendline("ps -ef|grep okex_eth|awk '{print $2}'|xargs kill -9")#执行命令
    sever.prompt()#等待命令执行完成
    sever.sendline("ps -ef|grep gdax_eth|awk '{print $2}'|xargs kill -9")#执行命令
    sever.prompt()#等待命令执行完成

    sever.sendline("nohup /usr/local/bin/python3 /home/hhy/fivehome3/binance_eth.py &")  # 执行命令
    sever.prompt()  # 等待命令执行完成

    sever.sendline("nohup /usr/local/bin/python3 /home/hhy/fivehome3/okex_eth.py &")  # 执行命令
    sever.prompt()  # 等待命令执行完成

    sever.sendline("nohup /usr/local/bin/python3 /home/hhy/fivehome3/huobi_eth.py &")  # 执行命令
    sever.prompt()  # 等待命令执行完成

    sever.sendline("nohup /usr/local/bin/python3 /home/hhy/fivehome3/gdax_eth.py &")  # 执行命令
    sever.prompt()  # 等待命令执行完成

    sever.sendline("nohup /usr/local/bin/python3 /home/hhy/fivehome3/bithumb_eth.py &")  # 执行命令
    sever.prompt()  # 等待命令执行完成





if __name__ == '__main__':
    while True:
        try:
            minute = datetime.datetime.now().minute
            second = datetime.datetime.now().second
            if (minute % 10 == 1 or minute % 10 == 6) and second in range(30,45) :
                aaa()
        except Exception as e:
                    print(e)
        time.sleep(5)
    #             t = int(time.mktime(time.strptime(f"{year}-{month}-{day} {hour}:{minute-5}:00", "%Y-%m-%d %H:%M:%S")))
    #             func(t)
    #             logger.info("要睡眠180秒")
    #             time.sleep(180)
    #         time.sleep(5)
    #     except Exception as e:
    #         logger.error(e)